package com.notecalendar.ws;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.xml.XMLSerializer;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.JsonSerializer;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.evernote.edam.notestore.NoteList;
import com.evernote.edam.type.Note;
import com.notecalendar.EvernoteBean;
import com.notecalendar.exception.EvernoteBeanException;
import com.notecalendar.ws.beans.EvernoteLogin;
import com.notecalendar.ws.beans.EvernoteLoginResponse;


@Controller
@RequestMapping("/evernotelogin")
public class EvernoteService {
	private static final String XML_VIEW_NAME_LOGIN = "evernoteloginresponse";
	private static final String VIEW_NOTELIST = "evernoteNoteListResponse";
	
	private Jaxb2Marshaller jaxb2Mashaller;
	
	public void setJaxb2Mashaller(Jaxb2Marshaller jaxb2Mashaller) {
		this.jaxb2Mashaller = jaxb2Mashaller;
	}
	
	//Injected via Spring
	private EvernoteBean evernoteBean;
	public void setEvernoteBean(EvernoteBean evernoteBean) {
		this.evernoteBean = evernoteBean;
	}
	
	
	@RequestMapping(method=RequestMethod.GET, value="/{id}")
	public ModelAndView getEvernoteId(@PathVariable String id){
		
		System.out.println("evernoteLogin -> id:"+id);
		
		EvernoteLoginResponse evernoteLoginResponse = new EvernoteLoginResponse();
			evernoteLoginResponse.setEvernote_id(id);
		
		return new ModelAndView(XML_VIEW_NAME_LOGIN, "evernoteloginresponse", evernoteLoginResponse);
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/login")
	public ModelAndView postEvernoteId(@RequestBody EvernoteLogin evernoteLogin){
	/*System.out.println("Body:"+body);
		body = "{'evernote_user':'user','evernote_pass':'pass'}"; 
		System.out.println("Body:"+body);
		XMLSerializer serializer = new XMLSerializer(); 
		 serializer.setRootName("evernote_login");
         serializer.setTypeHintsEnabled(false);
        JSON json = JSONSerializer.toJSON( body );
        body = serializer.write( json );  
        System.out.println("Body:"+body);
		
		Source source = new StreamSource(new StringReader(body));
		System.out.println("Source:"+source);
		
		
		EvernoteLogin evernoteLogin = (EvernoteLogin)jaxb2Mashaller.unmarshal(source);*/
		
		System.out.println("evernoteLogin -> user:"+evernoteLogin.getEvernote_user()+
				" pass:"+evernoteLogin.getEvernote_pass());
		
		EvernoteLoginResponse evernoteLoginResponse = new EvernoteLoginResponse();
			evernoteLoginResponse.setEvernote_id("evernote_id");
		
		return new ModelAndView(XML_VIEW_NAME_LOGIN, "evernoteloginresponse", evernoteLoginResponse);
	}
	
	
	
	/*@RequestMapping(method=RequestMethod.POST, value="/noteList")
	public ModelAndView postNoteList(@RequestBody String authContext){
		System.out.println("AuthContext:"+authContext);
		NoteList noteList = new NoteList();
		List<Note> nList = new ArrayList<Note>();
		try {
			noteList = evernoteBean.getNotes();
			nList = noteList.getNotes();
			System.out.println("NoteList:"+nList.size());
		} catch (EvernoteBeanException e) {
			e.printStackTrace();
		}
		
		return new ModelAndView(VIEW_NOTELIST, "notelist", nList);
	}
	
	@RequestMapping(method=RequestMethod.GET, value="/noteList")
	public ModelAndView getNoteList(){
		System.out.println("get AuthContext:");
		NoteList noteList = new NoteList();
		try {
			noteList = evernoteBean.getNotes();
			System.out.println("NoteList:"+noteList.getNotesSize());
		} catch (EvernoteBeanException e) {
			e.printStackTrace();
		}
		
		return new ModelAndView(VIEW_NOTELIST, "notelist", noteList);
	}
	*/
	
	
	
}
